import request from "@/js/axios.js";
import {useToast} from "vue-toastification";
import {useUserStore} from "@/store/userStore.js";

export let getAllStudent = (pageNo,pageSize,username) => {
    return request.get('/user/getAllStudent?pageNo='+pageNo+'&pageSize='+pageSize+'&username='+username)
        .then((res) => {
            if(res.data.code === 0) {
                return res.data.data;
            }
            throw "获取学生列表出错"
        }).catch((err) => {
            console.log(err);
            return false
        })
}

export let getAllTeacher = (pageNo,pageSize,username) => {
    return request.get('/user/getAllTeacher?pageNo='+pageNo+'&pageSize='+pageSize+'&username='+username)
        .then((res) => {
            if(res.data.code === 0) {
                return res.data.data;
            }
            throw "获取教师列表出错"
        }).catch((err) => {
            console.log(err);
            return false
        })
}

export let getAllAdmin = (pageNo,pageSize,username) => {
    return request.get('/user/getAllAdmin?pageNo='+pageNo+'&pageSize='+pageSize+'&username='+username)
        .then((res) => {
            if(res.data.code === 0) {
                return res.data.data;
            }
            throw "获取管理员列表出错"
        }).catch((err) => {
            console.log(err);
            return false
        })
}

export let findAllUser = () => {
    return request.get('/user/findAllUser')
        .then((res) => {
            if(res.data.code === 0) {
                return res.data.data;
            }
            throw "获取数据失败"
        }).catch((err) => {
            console.log(err);
            return false
        })
}


export let getUserByName = (username) => {
    return request.get(`/user/getUserInfo?username=${username}`)
        .then((res)=>{
            if(res.data.code==0){
                return res.data.data
            }
            throw "获取用户信息失败"
        }).catch((error)=>{
            return null
        })
}


export let getAllClass = () => {
    return request.get('/class/findAllClass')
        .then((res)=>{
            if(res.data.code==0){
                return res.data.data
            }
            throw "获取班级信息失败"
        }).catch((error)=>{
            return null
        })
}

export let addUserClass = (userId,classId) => {
    return request.post(`/userClass/addUserClass?userId=${userId}&classId=${classId}`).then((res)=>{
            if(res.data.code==0){
                return res.data.data
            }
            throw ""
        }).catch((error)=>{
            useToast().error("添加班级信息失败")
            return null
        })
}

export let deleteUser = (username) => {
    return request.post(`/user/deleteUserByUsername?username=${username}`).then(async (res) => {
        if (res.data.code == 0) {
            useToast().success("删除用户成功！")
            if (useUserStore().search_type === '学号') {
                await useUserStore().updateStudents(1, 5)
            } else if (useUserStore().search_type === '教师号') {
                await useUserStore().updateTeachers(1, 5)
            } else if (useUserStore().search_type === '管理员') {
                await useUserStore().updateAdmins(1, 5)
            }
            return res.data.data
        }
        throw ""
    }).catch((error)=>{
        useToast().error("删除用户失败")
        return false
    })
}